$(function () {
  //截取查询字符串
  //    ["?category", "0&category=%E7%94%B5%E8%A7%86&page", "1"]
  let data = { categoryid: 0, pageid: 1 };
  let arr = location.search.split('id=');
  data.categoryid = arr[1][0];
  data.pageid = arr[2];
  //   console.log(data);

  //渲染函数
  function render() {
    $.ajax({
      url: 'http://localhost:3000/api/getproductlist',
      data,
      success(res) {
        // console.log(res);
        $('.content ul').html(template('tmp', { list: res.result.list }));
      }
    });
  }
  render();

  //分页
  $('#selectPage').on('change', function () {
    // console.log($('#selectPage').val());
    data.pageid = $('#selectPage').val();
    render();
  });
  //上一页
  $('.prev').on('click', () => {
    let id = data.pageid;
    //首页不再发请求
    if (id == 1) return;
    id--;
    id = Math.max(id, 1);
    $('#selectPage').val(id);
    data.pageid = id;
    render();
  });
  //下一页
  $('.next').on('click', () => {
    let id = data.pageid;
    //末页不再发请求
    if (id == 3) return;
    id++;
    id = Math.min(id, 3);
    $('#selectPage').val(id);
    data.pageid = id;
    render();
  });
});
